<template>
  <div class="setting_pay">
    <div class="con">
      <div class="item">
        <span class="tip">免密</span>
        <span class="res">
          <van-switch v-model="isFree" size="20px" @change="switchIsFree" />
        </span>
      </div>

      <div class="item">
        <span class="tip">支付密码</span>
        <span class="res">
          <input
            :type="isShowPassword ? 'text' : 'password'"
            v-model="dataForm.payPassword"
            placeholder="支付密码"
            readonly
          />
          <van-icon
            name="closed-eye"
            v-show="!isShowPassword"
            @click="changeEys"
          />
          <van-icon name="eye-o" v-show="isShowPassword" @click="changeEys" />
        </span>
      </div>

      <div class="item">
        <span class="tip">修改密码</span>
        <span class="res">
          <van-button type="default" tag="span" size="small" @click="gotoReset">
            去设置 <van-icon name="arrow" />
          </van-button>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from "vuex";
import Vue from "vue";
import { Switch, Field, Button, Icon } from "vant";
import { PasswordInput, NumberKeyboard } from "vant";

Vue.use(Switch);
Vue.use(Field);
Vue.use(Button);
Vue.use(PasswordInput);
Vue.use(NumberKeyboard);
Vue.use(Icon);
export default {
  head() {
    return {
      title: "我的"
    };
  },
  data() {
    return {
      dataForm: {
        payPassword: "123456"
      },
      isShowPassword: false, // 是否限制密码数字
      isFree: null // isFree是否免密支付 0 表示免密，1 表示不免密
    };
  },

  created() {
    this.getPasswordInfo();
  },
  mounted() {
    console.log(this, 3333);
  },
  computed: {
    ...mapGetters({
      userInfo: "authen/userInfo",
      payPasswordInfo: "authen/payPasswordInfo"
    })
  },

  methods: {
    ...mapMutations({
      setPayPasswordInfo: "authen/setPayPasswordInfo"
    }),
    // 获取电子卡信息
    getPasswordInfo() {
      this.$axios
        .$get("/select", {
          params: {
            enterpriseId: this.enterpriseId,
            mobilePhone: this.userInfo.mobilePhone
          }
        })
        .then(res => {
          if (res.retCode === 0) {
            this.dataForm = res.data;
            this.setPayPasswordInfo(res.data);
            this.isFree = res.data.isFree === "0" ? true : false;
          } else {
            alert(res.message);
          }
        })
        .catch(err => {
          alert(err.message);
        });
    },

    changeEys() {
      this.isShowPassword = !this.isShowPassword;
    },
    gotoReset() {
      this.$router.push(`/${this.enterpriseId}/setting/reset_password`);
    },
    // 修改电子卡免密支付状态和密码
    switchIsFree(val) {
      console.log("开关：", val);
      let statusVal = val ? "0" : "1"; //  0 表示开启免密，1 表示关闭免密
      console.log(statusVal);
      this.$axios.$post("/update", {
        enterpriseId: this.enterpriseId,
        ids: this.payPasswordInfo.ids,
        isFree: statusVal,
        mobilePhone: this.userInfo.mobilePhone,
        payPassword: this.payPasswordInfo.payPassword
      });
    }
  }
};
</script>

<style lang="scss">
.setting_pay {
  width: 7.5rem;
  min-height: 100vh;
  background-color: #f5f6f7;
  padding: 0 0.29rem;
  box-sizing: border-box;
  overflow: hidden;

  > .con {
    box-sizing: border-box;
    margin-top: 0.3rem;

    .title {
      font-size: 0.3rem;
      font-weight: bold;
      line-height: 0.3rem;
    }
    .item {
      display: flex;
      justify-content: space-between;
      width: 6.87rem;
      height: 0.87rem;
      background: #ffffff;
      border-radius: 0.15rem;
      padding: 0 0.2rem;
      box-sizing: border-box;
      align-items: center;
      font-size: 0.28rem;
      font-weight: border;
      color: #000000;
      margin-top: 0.3rem;
      .res {
        display: flex;
        align-items: center;
        justify-content: center;
        // 眼睛
        .van-icon-closed-eye,
        .van-icon-eye-o {
          font-size: 0.4rem;
        }
        input {
          background: none;
          outline: none;
          border: none;
          text-align: right;
        }

        // 去设置css
        .van-button {
          border: none;
          background-color: transparent;
          padding: 0px;
          .van-button__content {
            .van-button__text {
              display: flex;
              align-items: center;
              justify-content: center;
              color: #999;
            }
          }
        }
      }

      // 密码框
      .van-cell {
        overflow: hidden;
        background: unset;
      }
    }
  }
}
</style>
